Imports BlazeApps.Library.Resources
Imports System.Data

Partial Class Usercontrols_Options_Attachments
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

    Public Property ItemGuid() As Guid
        Get
            Return GetGuidFromVS("ItemGuid")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("ItemGuid", value)
        End Set
    End Property

    Public Property InitialLoad() As Boolean
        Get
            Return GetBooleanFromVS("InitialLoad")
        End Get
        Set(ByVal value As Boolean)
            ViewState.Add("InitialLoad", value)
        End Set
    End Property

    Public Property IsDeleted() As Boolean
        Get
            Return GetBooleanFromVS("IsDeleted")
        End Get
        Set(ByVal value As Boolean)
            ViewState.Add("IsDeleted", value)
        End Set
    End Property

#End Region

#Region " Page Events "

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If ItemGuid = Guid.Empty Then
            GetQueryStringValues()
        End If

		'If Me.Page.IsPostBack = False Then
		BindDataItems()
		'End If

    End Sub

#End Region

#Region " Save Data "

#End Region

#Region " Load Data "

    Public Overloads Overrides Sub BindDataItems()
        If Not (ItemGuid = Guid.Empty) Then
            Dim List As New ResourceList
            Dim DT As New DataTable

            DT = List.GetResourceListByItem(ItemGuid).Tables(0)
            If DT.Rows.Count > 0 OrElse IsDeleted = True Then
                With Me.uxAttachmentsRepeater
                    .DataSource = DT
                    .DataBind()
                End With
            Else
                uxTemplate.Visible = False
            End If
        End If
    End Sub

#End Region

#Region " Form Controls Events "

    Protected Sub DeleteAttachment(ByVal sender As Object, ByVal e As CommandEventArgs)
        ItemResource.DeleteItemResource(ItemGuid, ConvertToGuid(e.CommandArgument.ToString))
        IsDeleted = True
        BindDataItems()
    End Sub

    Protected Sub uxAttachmentsRepeater_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles uxAttachmentsRepeater.ItemDataBound
        Dim LB As LinkButton = CType(e.Item.FindControl("uxDeleteAttachmentsLinkButton"), LinkButton)

        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then

            If IsUserAuthenticated Then
                If CurrentUserID = ConvertToGuid(e.Item.DataItem("userid").ToString().Trim()) Then
                    LB.Visible = True
                    LB.Enabled = True
                End If
            End If

        End If

    End Sub

#End Region

#Region " Helpers and Methods "

    Protected Sub GetQueryStringValues()
        ItemGuid = GetGuidFromQueryString("id")

        If ItemGuid = Guid.Empty Then
            ItemGuid = GetGuidFromQueryString("postid")
        End If

    End Sub

#End Region

End Class
